package com.ejuornal.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import com.ejuornal.bean.Artical;
import com.ejuornal.bean.Comment;
import com.ejuornal.bean.Page;
import com.ejuornal.dao.CommentDao;

@Component("commentDao")
public class CommentDaoImpl extends DaoSupport<Comment> implements CommentDao {

	public void add(String sql, Object... objs) {
		super.add(sql, objs);

	}

	public void batchDelete(String sql, int... ids) {
		super.batchDelete(sql, ids);

	}

	public List<Comment> findAllByTypeId(int typeId) {
		String sql = "select * from t_comment where typeId = ?";
		List<Comment> list = super.getJdbcTemplate().query(sql,
				new Object[] { typeId }, new RowMapper<Comment>() {
					public Comment mapRow(ResultSet rs, int rowNum)
							throws SQLException {
						Comment comment = new Comment();
						comment.setCommDate(rs.getDate("commDate"));
						comment.setCommentId(rs.getInt("commentId"));
						comment.setContent(rs.getString("content"));
						comment.setOpinion(rs.getString("opinion"));
						comment.setScore(rs.getFloat("score"));
						comment.setType(rs.getString("type"));
						comment.setTypeId(rs.getInt("typeId"));
						comment.setUserId(rs.getInt("userId"));
						return comment;
					}
				});
		return list;
	}

	public Comment detailInfoComment(int id) {
		String sql = "select * from t_comment where commentId = ?";
		Comment comment = super.getJdbcTemplate().queryForObject(sql,
				new Object[] { id }, new RowMapper<Comment>() {
					public Comment mapRow(ResultSet rs, int rowNum)
							throws SQLException {
						Comment commentInfo = new Comment();
						commentInfo.setCommDate(rs.getDate("commDate"));
						commentInfo.setCommentId(rs.getInt("commentId"));
						commentInfo.setContent(rs.getString("content"));
						commentInfo.setOpinion(rs.getString("opinion"));
						commentInfo.setScore(rs.getFloat("score"));
						commentInfo.setType(rs.getString("type"));
						commentInfo.setTypeId(rs.getInt("typeId"));
						commentInfo.setUserId(rs.getInt("userId"));
						return commentInfo;
					}
				});
		return comment;
	}
	
	public Page<Comment> find(int current_record, String url, int pageSize, String sql_1, String sql_2, Object[] objs) {
		Page<Comment> page = new Page<Comment>();
		@SuppressWarnings("deprecation")
		int totalRecords = this.getJdbcTemplate().queryForInt(sql_1, objs);
		List<Comment> list = (List<Comment>) this.getJdbcTemplate().query(sql_2, objs, new CommentMapper());
        page.setPageSize(pageSize);
        page.setUrl(url);
        page.setTotalRecords(totalRecords);
        page.setTotalPage(totalRecords);
        page.setRecords(list);
        
		return page;
	}
	
	public Page<Comment> find(int current_record, String url, String sql_1, String sql_2, Object[] objs) {
		return find(current_record, url, 10, sql_1, sql_2, objs);
	}
	
	public List<Comment> findComment(String sql, Object[] objs) {
		List<Comment> comments = super.getJdbcTemplate().query(sql, objs, new RowMapper<Comment>() {
			@Override
			public Comment mapRow(ResultSet rs, int rowNum) throws SQLException {
				Artical artical = new Artical();
				artical.setTitle(rs.getString("title"));
				artical.setPerPeriodicalId(rs.getInt("perPeriodicalId"));
				
				Comment comment = new Comment();
				comment.setCommentId(rs.getInt("commentId"));
				comment.setContent(rs.getString("content"));
				comment.setCommDate(rs.getDate("commDate"));
				comment.setArtical(artical);
				
				return comment;
			}
		});
		
		return comments;
	}
}

final class CommentMapper implements RowMapper<Comment> {

	@Override
	public Comment mapRow(ResultSet rs, int rowNum) throws SQLException {
		Artical artical = new Artical();
		artical.setArticalId(rs.getInt("articalId"));
		artical.setBoardId(rs.getInt("boardId"));
		artical.setCnAbstract(rs.getString("cnAbstract"));
		artical.setContent(rs.getString("content"));
		artical.setEnAbstract(rs.getString("enAbstract"));
		artical.setKeywords(rs.getString("keywords"));
		artical.setPerPeriodicalId(rs.getInt("perPeriodicalId"));
		artical.setPostDate(rs.getDate("postDate"));
		artical.setPubDate(rs.getDate("pubDate"));
		artical.setRefDoc(rs.getString("refDoc"));
		artical.setStatus(rs.getString("status"));
		artical.setTitle(rs.getString("title"));
		artical.setUserId(rs.getInt("userId"));
		artical.setViewCount(rs.getInt("viewCount"));
		
		Comment comment = new Comment();
		comment.setCommDate(rs.getDate("commDate"));
		comment.setCommentId(rs.getInt("commentId"));
		comment.setContent(rs.getString("ccontent"));
		comment.setScore(rs.getFloat("score"));
		comment.setArtical(artical);
		
		return comment;
	}
	
}
